 '   Copyright (C) 2011  Jan-Willem de Bruyn

'   This program is free software: you can redistribute it and/or modify
'   it under the terms of the GNU General Public License as published by
'   the Free Software Foundation, either version 3 of the License, or
'   (at your option) any later version.
'
'   This program is distributed in the hope that it will be useful,
'   but WITHOUT ANY WARRANTY; without even the implied warranty of
'    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
'    GNU General Public License for more details.

'    You should have received a copy of the GNU General Public License
'    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
Imports System.IO
Public Class Form1
    Dim opend As Boolean = False
    Public input2 As Stream
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim streamread4 As StreamReader
        If opend = False Then
            streamread4 = New StreamReader("database.psy")
        Else
            streamread4 = New StreamReader(input2)
        End If
        Dim middle() As String = streamread4.ReadToEnd.Split(";")
        Dim str42 As String
        Dim str43 As String
        Dim str4 As String = middle.GetValue(0)
        Try
            str42 = middle.GetValue(1)
            str43 = middle.GetValue(2)
        Catch ex As Exception
            str42 = ""
            str43 = ""
        End Try
        streamread4.Close()
        Dim stream4 As StreamWriter
        If opend = False Then
            stream4 = New StreamWriter("database.psy")
        Else
            stream4 = New StreamWriter(input2)
        End If
        Dim txtbox4 As String = TextBox1.Text
        Dim txtbox42 As String = TextBox2.Text
        Dim txtbox43 As String = TextBox3.Text
        txtbox4 = SimpleCrypt(txtbox4)
        txtbox42 = SimpleCrypt(txtbox42)
        txtbox43 = SimpleCrypt(txtbox43)
        stream4.WriteLine(str4 & "," & txtbox4 & ";" & str42 & "," & txtbox42 & ";" & str43 & "," & txtbox43)
        stream4.Close()
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim streamread As StreamReader
        ListBox1.Items.Clear()
        If opend = False Then
            streamread = New StreamReader("database.psy")
        Else
            streamread = New StreamReader(input2)
        End If
            Dim input As String = streamread.ReadToEnd
            Dim middle() As String = input.Split(";")
            Dim str1() As String = middle.GetValue(0).ToString.Split(",")

            Dim num As Integer = 0
            For Each Str As String In str1
                num += 1
                If num > 1 Then
                    ListBox1.Items.Add(SimpleCrypt(Str))
                End If
            Next
            streamread.Close()
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Dim stream As StreamWriter
        If opend = False Then
            stream = New StreamWriter("database.psy")
        Else
            stream = New StreamWriter(input2)
        End If
        stream.WriteLine()
        stream.Close()
    End Sub

    Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
        Dim streamread As StreamReader
        If opend = False Then
            streamread = New StreamReader("database.psy")
        Else
            streamread = New StreamReader(input2)
        End If
        Dim input As String = streamread.ReadToEnd
        streamread.Close()
        Dim middle() As String = input.Split(";")
        Dim str1() As String = middle.GetValue(1).ToString.Split(",")
        Dim replys As DialogResult = MessageBox.Show(SimpleCrypt(str1.GetValue(ListBox1.SelectedIndex + 1)) & " do you want download link?", "info", MessageBoxButtons.YesNo)
        Dim reply As DialogResult
        Dim str2() As String = middle.GetValue(2).ToString.Split(",")
        If replys = DialogResult.Yes Then
            reply = MessageBox.Show(SimpleCrypt(str2.GetValue(ListBox1.SelectedIndex + 1)) & " goto link?", "download link", MessageBoxButtons.OKCancel)
        End If
        If reply = DialogResult.Yes Then
            If SimpleCrypt(str2.GetValue(ListBox1.SelectedIndex + 1).ToString.StartsWith("http://")) & SimpleCrypt(str2.GetValue(ListBox1.SelectedIndex + 1).ToString.EndsWith("/")) Then
                System.Diagnostics.Process.Start(SimpleCrypt(str2.GetValue(ListBox1.SelectedIndex + 1)))
            End If
        End If
    End Sub

    Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click
        Dim streamread As StreamReader
        If opend = False Then
            streamread = New StreamReader("database.psy")
        Else
            streamread = New StreamReader(input2)
        End If
        ListBox2.Items.Clear()
        Dim input As String = streamread.ReadToEnd
        streamread.Close()
            Dim middle() As String = input.Split(";")
            Dim str1() As String = middle.GetValue(0).ToString.Split(",")
            Dim num As Integer = 0
            For Each Str As String In str1
                num += 1
                If num > 1 Then
                    ListBox2.Items.Add(SimpleCrypt(Str))
                End If
            Next
    End Sub

    Private Sub ListBox2_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox2.SelectedIndexChanged
        Dim streamread As StreamReader
        If opend = False Then
            streamread = New StreamReader("database.psy")
        Else
            streamread = New StreamReader(input2)
        End If
        Dim input As String = streamread.ReadToEnd
        streamread.Close()
        Dim middle() As String = input.Split(";")
        Dim str() As String = middle.GetValue(0).ToString.Split(",")
        Dim str1() As String = middle.GetValue(1).ToString.Split(",")
        Dim str2() As String = middle.GetValue(2).ToString.Split(",")

        TextBox1.Text = SimpleCrypt(str.GetValue(ListBox2.SelectedIndex + 1))
        TextBox2.Text = SimpleCrypt(str1.GetValue(ListBox2.SelectedIndex + 1))
        TextBox3.Text = SimpleCrypt(str2.GetValue(ListBox2.SelectedIndex + 1))
        TabControl1.SelectTab(0)
    End Sub
    Public Function SimpleCrypt( _
       ByVal Text As String) As String
        ' Encrypts/decrypts the passed string using 
        ' a simple ASCII value-swapping algorithm
        Dim strTempChar As String, i As Integer
        For i = 1 To Len(Text)
            If Asc(Mid$(Text, i, 1)) < 128 Then
                strTempChar = _
          CType(Asc(Mid$(Text, i, 1)) + 128, String)
            ElseIf Asc(Mid$(Text, i, 1)) > 128 Then
                strTempChar = _
          CType(Asc(Mid$(Text, i, 1)) - 128, String)
            End If
            Mid$(Text, i, 1) = _
                Chr(CType(strTempChar, Integer))
        Next i
        Return Text
    End Function

    Private Sub GotoWebsiteToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GotoWebsiteToolStripMenuItem1.Click
        System.Diagnostics.Process.Start("http://www.cobosoft.com/")
    End Sub

    Private Sub GotoWebsiteToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GotoWebsiteToolStripMenuItem.Click
        System.Diagnostics.Process.Start("http://www.psyme.tk/")
    End Sub

    Private Sub NewToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NewToolStripMenuItem.Click
        Dim stream As Stream
        SaveFileDialog1.Filter = "psy files (*.psy)|*.psy"
        SaveFileDialog1.FilterIndex = 1
        SaveFileDialog1.RestoreDirectory = True
        If SaveFileDialog1.ShowDialog() = DialogResult.OK Then
            stream = SaveFileDialog1.OpenFile()
            Dim strwrite As StreamWriter = New StreamWriter(stream)
            strwrite.WriteLine()
        End If
    End Sub

    Public Sub OpenToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OpenToolStripMenuItem.Click
        OpenFileDialog1.ShowDialog()
        input2 = OpenFileDialog1.OpenFile
        opend = True
    End Sub

    Private Sub CloseToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CloseToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub SvnToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SvnToolStripMenuItem.Click
        svn.ShowDialog()
    End Sub
End Class
